package com.slmxz.tms.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.slmxz.tms.biz.model.vo.TransitVO;
import com.slmxz.tms.common.annotation.Log;
import com.slmxz.tms.common.enums.LogModuleEnum;
import com.slmxz.tms.common.model.Query;
import com.slmxz.tms.common.support.Condition;
import com.slmxz.tms.system.service.ConfigService;
import com.slmxz.tms.common.result.PageResult;
import com.slmxz.tms.common.result.Result;
import com.slmxz.tms.system.model.form.ConfigForm;
import com.slmxz.tms.system.model.query.ConfigPageQuery;
import com.slmxz.tms.system.model.vo.ConfigVO;
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;


/**
 * 系统配置前端控制层
 * @author sunyao
 * @Date 2024-11-08
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@Tag(name = "10.系统配置")
@RequestMapping("/system/config")
public class ConfigController {

    private final ConfigService configService;

    @GetMapping("/page")
    @Operation(summary = "系统配置分页列表")
    @PreAuthorize("@ss.hasPerm('sys:config:query')")
    public PageResult<ConfigVO> page(@ParameterObject Query query, ConfigVO config) {
        IPage<ConfigVO> result = this.configService.selectPage(Condition.getPage(query),config);
        return PageResult.success(result);
    }

    @Operation(summary = "新增系统配置")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('sys:config:add')")
    @Log(value= "新增系统配置",module = LogModuleEnum.CONFIG)
    public Result<?> save(@RequestBody @Valid ConfigForm configForm) {
        return Result.result(configService.save(configForm));
    }



    @Operation(summary = "获取系统配置表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('sys:config:query')")
    public Result<ConfigForm> getConfigForm(
            @Parameter(description = "系统配置ID") @PathVariable Long id
    ) {
        ConfigForm formData = configService.getConfigFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "获取系统配置的值")
    @GetMapping("/selectConfigByKey/{configKey}")
    @PreAuthorize("@ss.hasPerm('sys:config:query')")
    public Result<?> selectConfigByKey(@Parameter(description = "系统配置KEY") @PathVariable String configKey) {
        return Result.success(this.configService.selectConfigByKey(configKey));
    }


    @Operation(summary = "刷新系统配置缓存")
    @PatchMapping
    @PreAuthorize("@ss.hasPerm('sys:config:refresh')")
    public Result<ConfigForm> refreshCache() {
        return Result.result(configService.refreshCache());
    }

    @PutMapping(value = "/update")
    @Operation(summary = "修改系统配置")
    @PreAuthorize("@ss.hasPerm('sys:config:edit')")
    @Log(value= "修改系统配置",module = LogModuleEnum.CONFIG)
    public Result<?> update(@Valid @RequestBody ConfigForm configForm) {
        return Result.result(configService.edit(configForm));
    }

    @Operation(summary = "删除系统配置信息")
    @DeleteMapping("/remove/{ids}")
    @PreAuthorize("@ss.hasPerm('sys:config:delete')")
    @Log(value= "删除系统配置",module = LogModuleEnum.CONFIG)
    public Result<Void> deleteDrivers(
            @Parameter(description = "司机信息ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = this.configService.deleteBatch(ids);
        return Result.result(result);
    }

}
